/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.PageBean;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.guide.entity.QueueCount;
import com.jeeplus.modules.guide.entity.YmbcsUser;
import com.jeeplus.modules.guide.entity.YmbcsValue;
import com.jeeplus.modules.guide.mapper.MobileQueueMapper;
import com.jeeplus.modules.guide.mapper.MobileSilentMapper;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@DS("queuing")
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class MobileSilentService  extends BaseController {
    private final MobileSilentMapper mobileSilentMapper;
    private final MobileQueueMapper mobileQueueMapper;
    //查询排队相关数据
    public Map<String, Object> getQueueValue(String number, String areaId) {
        //排队情况统计
        int waitTime = 10;
        Map<String, Object> charsId = mobileSilentMapper.findChars(number,areaId);
        Map<String, Object> map = mobileSilentMapper.findWaitTimeAndNum(number, areaId, String.valueOf(charsId.get("typeId")));
        if(map == null)map = new HashMap<String, Object>();
        //无等待时间进行判断处理
        if(null == map.get("waitTime") || "".equals(map.get("waitTime"))){
            map.put("waitTime","暂无数据");
        }else{
            waitTime = Integer.parseInt(map.get("waitTime").toString());
        }
        //判断是否过号
        String wino = mobileQueueMapper.getWinoValue(areaId,number);
        if(StringUtils.isNotBlank(wino)){
            map.put("num","已过号");
            map.put("wino",wino);
            map.put("waitDoTime",0);
        }else{
            //判断业务可办窗口  预计时间统计  统计该排队编号可办窗口 再统计可办窗口所办理的排队编号 按取号时间进行排队统计
            String typeValue = String.valueOf(charsId.get("typeValue"));
            if(typeValue.length() == 1)typeValue = "00"+typeValue;
            else if(typeValue.length() == 2)typeValue = "0"+typeValue;
            List<QueueCount> waitTimeAndNumWin = mobileSilentMapper.findWaitTimeAndNumWin(areaId, typeValue);
            //窗口统计
            Set<String> winList = new HashSet <String>();
            String[] splitList;
            for(QueueCount qc : waitTimeAndNumWin){
                if(StringUtils.isNotBlank(qc.getWinA())){
                    if(qc.getWinA().indexOf("-") >= 0){
                        splitList = qc.getWinA().split("-");
                        for(String str : splitList){
                            if(str.indexOf("00") >= 0)winList.add(str.replaceFirst("00",""));
                            else winList.add(str.replaceFirst("0",""));
                        }
                    }else{
                        if(qc.getWinA().indexOf("00") >= 0)winList.add(qc.getWinA().replaceFirst("00",""));
                        else winList.add(qc.getWinA().replaceFirst("0",""));
                    }
                }
                if(StringUtils.isNotBlank(qc.getWinB())){
                    if(qc.getWinB().indexOf("-") >= 0){
                        splitList = qc.getWinB().split("-");
                        for(String str : splitList){
                            if(str.indexOf("00") >= 0)winList.add(str.replaceFirst("00",""));
                            else winList.add(str.replaceFirst("0",""));
                        }
                    }else{
                        if(qc.getWinB().indexOf("00") >= 0)winList.add(qc.getWinB().replaceFirst("00",""));
                        else winList.add(qc.getWinB().replaceFirst("0",""));
                    }

                }
                if(StringUtils.isNotBlank(qc.getWinC())){
                    if(qc.getWinC().indexOf("-") >= 0){
                        splitList = qc.getWinC().split("-");
                        for(String str : splitList){
                            if(str.indexOf("00") >= 0)winList.add(str.replaceFirst("00",""));
                            else winList.add(str.replaceFirst("0",""));
                        }
                    }else{
                        if(qc.getWinC().indexOf("00") >= 0)winList.add(qc.getWinC().replaceFirst("00",""));
                        else winList.add(qc.getWinC().replaceFirst("0",""));
                    }
                }
                if(StringUtils.isNotBlank(qc.getWinD())){
                    if(qc.getWinD().indexOf("-") >= 0){
                        splitList = qc.getWinD().split("-");
                        for(String str : splitList){
                            if(str.indexOf("00") >= 0)winList.add(str.replaceFirst("00",""));
                            else winList.add(str.replaceFirst("0",""));
                        }
                    }else{
                        if(qc.getWinD().indexOf("00") >= 0)winList.add(qc.getWinD().replaceFirst("00",""));
                        else winList.add(qc.getWinD().replaceFirst("0",""));
                    }

                }
                if(StringUtils.isNotBlank(qc.getWinE())){
                    if(qc.getWinE().indexOf("-") >= 0){
                        splitList = qc.getWinE().split("-");
                        for(String str : splitList){
                            if(str.indexOf("00") >= 0)winList.add(str.replaceFirst("00",""));
                            else winList.add(str.replaceFirst("0",""));
                        }
                    }else{
                        if(qc.getWinE().indexOf("00") >= 0)winList.add(qc.getWinE().replaceFirst("00",""));
                        else winList.add(qc.getWinE().replaceFirst("0",""));
                    }
                }
            }
            //窗口可办业务统计 在计算预计等待时间 办件数/窗口数*平均等待时间 保留整数
            Integer queueCount = mobileSilentMapper.findQueueCount(mobileSilentMapper.findTypeIDByWinno(areaId,new ArrayList <String>(winList)), mobileSilentMapper.findNumberTime(areaId, number));
            DecimalFormat da = new DecimalFormat("#0");
            String waitDoTime = da.format(Double.valueOf(queueCount)/waitTimeAndNumWin.size()*waitTime);
            if(Integer.parseInt(waitDoTime) <= 0)waitDoTime = "1";
            map.put("waitDoTime",waitDoTime);
            map.put("num",queueCount);
        }
        return  map;
    }
    //群众访问记录添加 status=1是语音拨打， 0不是
    @Transactional(readOnly = false)
    public Boolean insertValue(String number, String areaId, String openid, String subscribe, String nickname, String tell,int status) {
        //当天群众访问记录保存
        //查询是否有记录
        try{
            HashMap<String,String> result = mobileSilentMapper.findWXUser(DateUtils.getDateTime(),number,areaId,status);
            if(null == result || null == result.get("tell")){
                mobileSilentMapper.inserWxUser(number, areaId, DateUtils.getDateTime(), openid, subscribe, nickname,tell,status);
            }else if(null == result.get("openid")){
                mobileSilentMapper.updateWxUser(openid, subscribe, nickname,DateUtils.getDateTime(),number,areaId,status);
            }
            if(null == result || null == result.get("openid")){
                return true;
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return false;

    }

    //群众网上取号访问记录添加 status=1是语音拨打， 0不是
    @Transactional(readOnly = false)
    public void insertGXValue(String number, String areaId, String openid, String subscribe, String nickname, String tell,int status) {
        mobileSilentMapper.inserWxUser(number, areaId, DateUtils.getDateTime(), openid, subscribe, nickname,tell,status);

    }

    //叫号信息查询告知
    public List<Map<String,Object>> selectQueueByNumber(String areaId, String number){
        return mobileSilentMapper.selectQueueByNumber(areaId,number);
    }

    //排队信息告知
    public List<Map<String,Object>> selectLineByNumber(String number,String areaId){
        return mobileSilentMapper.selectLineByNumber(number,areaId);
    }
    //无声叫号记录
    @Transactional(readOnly = false)
    public void intoUserHistory(int type,int waitNum,String tell,String areaId,String number,String winNo,String userNo,String idcard,String yyID,String result){
        mobileSilentMapper.intoUserHistory(type, waitNum, tell, areaId, number, winNo, userNo, idcard, yyID, DateUtils.getDateTime(),result);
    }

    //获取大厅楼层信息
    public String findAreaNameValue(String areaId){
        return mobileSilentMapper.findAreaNameValue(areaId);
    }

    //添加发送记录
    @Transactional(readOnly = false)
    public void intoTellCount(String tell,String doTime,String number,String areaId){
        mobileSilentMapper.intoTellCount(tell,doTime,number,areaId);
    }
    //查询发送记录
    public Integer getTellCount(String tell,String areaId,String number){
        return mobileSilentMapper.getTellCount(tell,areaId,number);
    }
    //删除当天记录
    @Transactional(readOnly = false)
    public void deleteTellCount(){
        mobileSilentMapper.deleteTellCount();
    }

    public HashMap<String,Object> findWSValueByAreaId(String typeId){
        return mobileSilentMapper.findWSValueByAreaId(typeId);
    }

    public String findWSWino(String areaId,String typeNumber){
        if(StringUtils.isNotBlank(typeNumber)){
            if(typeNumber.length() == 1){
                typeNumber = "00"+typeNumber;
            }else if(typeNumber.length() == 2){
                typeNumber = "0"+typeNumber;
            }
        }
        typeNumber = "%"+typeNumber+"%";
        return mobileSilentMapper.findWSWino(areaId,typeNumber);
    }

    public HashMap<String,String> findAreaNameValueByNumber(String number){
        return mobileSilentMapper.findAreaNameValueByNumber(number);
    }

    public HashMap<String,Object> findWSValueByAreaIdByNumber(String number){
        return mobileSilentMapper.findWSValueByAreaIdByNumber(number);
    }
    @Transactional(readOnly = false)
    public void insertOnLine(String number, String areaId, String openid, String subscribe, String nickname, String tell,int status) {
        mobileSilentMapper.insertOnLine(number, areaId, DateUtils.getDateTime(), openid, subscribe, nickname,tell,status);

    }

    public int findCountToYY(String areaId,String number){
        return mobileSilentMapper.findCountToYY(areaId,number);
    }
    public int selectNumberCount(String areaId,String number){
        return mobileSilentMapper.selectNumberCount(areaId,number);
    }

    public List<Integer> yyListValueQD(String startTime,String endTime){
        return mobileSilentMapper.yyListValueQD(startTime,endTime);
    }

    public List<Integer> yyListValue(String type){
        if("all".equals(type)){
            // type = all 全量
            return mobileSilentMapper.yyListValue();
        }else if("day".equals(type)){
            // type = day 当天
            return mobileSilentMapper.yyListValueDay();
        }else if("week".equals(type)){
            // type = week 7天
            return mobileSilentMapper.yyListValueWeek();
        }else if("month".equals(type)){
            // type = month 30天
            return mobileSilentMapper.yyListValueMonth();
        }else if("threeMonth".equals(type)){
            // type = threeMonth 3个月
            return mobileSilentMapper.yyListValueThreeMonth();
        }else{
            // type = year 12月
            return mobileSilentMapper.yyListValueYear();
        }
    }

    public List<Integer> ymbcsValueListQD(String startTime,String endTime){
        return mobileSilentMapper.ymbcsValueListQD(startTime,endTime);
    }

    public List<Integer> ymbcsValueList(String type){
        if("all".equals(type)){
            // type = all 全量
            return mobileSilentMapper.ymbcsValueList();
        }else if("day".equals(type)){
            // type = day 当天
            return mobileSilentMapper.ymbcsValueListDay();
        }else if("week".equals(type)){
            // type = week 7天
            return mobileSilentMapper.ymbcsValueListWeek();
        }else if("month".equals(type)){
            // type = month 30天
            return mobileSilentMapper.ymbcsValueListMonth();
        }else if("threeMonth".equals(type)){
            // type = threeMonth 3个月
            return mobileSilentMapper.ymbcsValueListThreeMonth();
        }else{
            // type = year 12月
            return mobileSilentMapper.ymbcsValueListYear();
        }
    }

    public HashMap<String,String>selectYMBCSValue(String idcard,String doTime){
        return  mobileSilentMapper.selectYMBCSValue(idcard,doTime);
    }

    public String ymbcsEndTime(String number,String serviceTime){
        return mobileSilentMapper.ymbcsEndTime(number,serviceTime);
    }

    /**
     * 获取一码办成事取号数据
     * gxbListValue
     * @return
     */
    public PageBean <YmbcsUser> ymbcsQueueList(String pageNo, String pageSize, String type){
        if(com.jeeplus.common.utils.StringUtils.isBlank(pageSize))pageSize="5";
        if(com.jeeplus.common.utils.StringUtils.isBlank(pageNo))pageNo="1";
        PageBean <YmbcsUser> pageBean = new PageBean<YmbcsUser>();
        PageHelper.startPage(Integer.valueOf(pageNo) , Integer.valueOf(pageSize));
        List<YmbcsUser> list = null;
        if("all".equals(type)){
            list = mobileSilentMapper.ymbcsQueueList();
        }else if("day".equals(type)){
            list = mobileSilentMapper.ymbcsQueueListDay();
        }else if("week".equals(type)){
            list = mobileSilentMapper.ymbcsQueueListWeek();
        }else if("month".equals(type)){
            list = mobileSilentMapper.ymbcsQueueListMonth();
        }else if("threeMonth".equals(type)){
            list = mobileSilentMapper.ymbcsQueueListThreeMonth();
        }else if("year".equals(type)){
            list = mobileSilentMapper.ymbcsQueueListYear();
        }
        PageInfo <YmbcsUser> pageInfo = new PageInfo<YmbcsUser>(list);
        pageBean.setCrurentNum(Integer.valueOf(pageNo));
        pageBean.setPageSize(Integer.valueOf(pageSize));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    /**
     * 获取一码办成事取号部门点位数据
     * gxbListValue
     * @return
     */
    public YmbcsUser findYmbcsValue(String areaId,String number,String serviceTime){
        return mobileSilentMapper.findYmbcsValue(areaId,number,serviceTime);
    }

    /**
     * 获取一码办成事取号详细数据
     * gxbListValue
     * @return
     */
    public List <YmbcsValue> ymbcsQueueValueById(String userId){
        return mobileSilentMapper.ymbcsQueueValueById(userId);
    }

    /**
     * 获取一码办成事查询数据
     * gxbListValue
     * @return
     */
    public PageBean <YmbcsUser> ymbcsFindList(String pageNo, String pageSize, String type){
        if(com.jeeplus.common.utils.StringUtils.isBlank(pageSize))pageSize="5";
        if(com.jeeplus.common.utils.StringUtils.isBlank(pageNo))pageNo="1";
        PageBean <YmbcsUser> pageBean = new PageBean<YmbcsUser>();
        PageHelper.startPage(Integer.valueOf(pageNo) , Integer.valueOf(pageSize));
        List<YmbcsUser> list = null;
        if("all".equals(type)){
            list = mobileSilentMapper.ymbcsFindList();
        }else if("day".equals(type)){
            list = mobileSilentMapper.ymbcsFindListDay();
        }else if("week".equals(type)){
            list = mobileSilentMapper.ymbcsFindListWeek();
        }else if("month".equals(type)){
            list = mobileSilentMapper.ymbcsFindListMonth();
        }else if("threeMonth".equals(type)){
            list = mobileSilentMapper.ymbcsFindListThreeMonth();
        }else if("year".equals(type)){
            list = mobileSilentMapper.ymbcsFindListYear();
        }
        PageInfo <YmbcsUser> pageInfo = new PageInfo<YmbcsUser>(list);
        pageBean.setCrurentNum(Integer.valueOf(pageNo));
        pageBean.setPageSize(Integer.valueOf(pageSize));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }


    public HashMap<String,String> findWXPJValue(String id){
        return mobileSilentMapper.findWXPJValue(id);
    }

    public List <YmbcsValue> ymbcsFindValueById(String userId){
        return mobileSilentMapper.ymbcsFindValueById(userId);
    }


    //人才通业务评价推送 当天
    public List<Map<String,String>> pjListByRCTDay(){
        return mobileSilentMapper.pjListByRCTDay();
    }

    //人才通业务评价推送 所有
    public List<Map<String,String>> pjListByRCTAll(){
        return mobileSilentMapper.pjListByRCTAll();
    }

    //人才通业务评价 数据添加
    @Transactional(readOnly = false)
    public void pjListByRCTInto(String cardid,String typeName,String serverTime,String pjMark,String pjLevel){
        mobileSilentMapper.pjListByRCTInto(cardid,typeName,serverTime,pjMark,pjLevel);
    }
}
